home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13526 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  111 lines

  1. Path: dimis.fage.gr!e-ID: <3168FD0A.323F@fage.gr>
  2. From: "E. Lefty Kreouzis" <lefty@fage.gr>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Recursion
  5. Date: Mon, 08 Apr 1996 14:48:26 +0300
  6. Organization: FAGE S.A.
  7. Message-ID: <4kau8o$o46@brigitte.forthnet.gr>
  8. References: <31624BC2.70D2@sooner.net> <4k1vbg$6gu@newshost.cyberramp.net>
  9. NNTP-Posting-Host: dimis.fage.gr
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01Gold (Win95; I)
  14.  
  15. John L. Noland wrote:
  16. > n article <31624BC2.70D2@sooner.net>, edwbush@sooner.net says...
  17. > >
  18. > >I am trying to construct a C function that will recursively convert
  19. > >a string such as "1234" into it's integer equivelant (1234).
  20. > >
  21. > >Here is what I know:
  22. > >1)if you subtract the character "0" from any of the other digits "1".."9"
  23. > >  you will get the integer value of that characer.
  24. > >        Example:  "1" - "0" is equal to 1
  25. > >                  "2" - "0" is equal to 2
  26. > >                  .
  27. > >                  .
  28. > >                  .
  29. > >                  "9" - "0" is equal to 9
  30. > >2)the function should be called with a character pointer:
  31. > >        Such as:   convert("1234");
  32. > >  making the prototype look something like:
  33. > >        int convert(char *p);
  34. > >
  35. > >Does anyone have an idea?  This is sorta stumping me.  I am aware of
  36. > >atoi, but I am wanting to write a recursive function that does that --
  37. > >for the fun of it.  It's sort of a little puzzle to help me learn
  38. > >recursion.  Any ideas?
  39. > >
  40. > >Thanx in Advance!
  41. > >
  42. > void convert (char *buf, int result);
  43. > int main (void) {
  44. > char str[] = "1234";
  45. > int  result = 0;
  46. >     convert(str, result);
  47. >     return 0;
  48. > }
  49. > void convert (char *buf, int result) {
  50. > int check_array[100]; /* Good for strings of numbers up to 100 digits long! */
  51. > int i = 0, j = 0;
  52. >     if (*buf = '\0') return;
  53. >     j = i;                              /*Save i */
  54. >     for (i = 0; i < 100; i++)
  55. >         check_array[i] = 0;             /*Initialize our array */
  56. >     i = j;                              /*Restore i */
  57. >     check_array[i] = (int)(*buf - '0'); /*Convert ASCII to integer and save */
  58. >     i += 1;                             /*increment i  */
  59. >     j = i;                              /*Save i for later*/
  60. >     for (i = 0; i < 100; i++)
  61. >         result += check_array[i];       /*tally up the numbers */
  62. >     i = j;                              /*restore i to previous */
  63. >     buf += i;                           /*advance our pointer */
  64. >     convert(buf, result);
  65. >     if (*buf = '\0') return;
  66. > }
  67. > This could probably be made a little more efficient, but I think this'll work
  68. > for you. Didn't test it though!
  69. > Helping with homework is good for the soul!
  70. > -John
  71.  
  72. hmm--- what is wrong with:
  73.  
  74. int convert(char *buff, int *p10)
  75. {    int partial;
  76.     int result;
  77.     
  78.     if ( !*buff )
  79.     {
  80.         p10 = 1;    
  81.         return 0;
  82.     }
  83.     partial=convert( buff+1, p10);
  84.     result=(*buff - '0')*p10 + partial;
  85.     *p10 *= 10;
  86.     return result;
  87. }
  88.  
  89. int main(void)
  90. {
  91.     char snum[]="1234";
  92.     int temp;
  93.  
  94.     printf("%s = %d", snum, convert(snum, temp);
  95.     return 0;
  96. }
  97.  
  98. Lefty.
  99.